<template>
  <div>
    <InfoTable ref="InfoTable" location="amends" :filterFields="filterFields">
      <template slot="table-operation" slot-scope="row">
        <b-btn variant="outline-primary" style="margin-right:8px;padding:6px;font-size:0.7rem" @click="info($refs.InfoTable,row)">详细信息</b-btn>
        <b-btn variant="outline-primary" style="margin-right:8px;padding:6px;font-size:0.7rem" @click="defectManagerModalShow($refs.InfoTable,row)">bug管理</b-btn>
        <b-btn variant="outline-primary" style="margin-right:8px;padding:6px;font-size:0.7rem" @click="remove($refs.InfoTable,row)">删除</b-btn>
      </template>
      <template slot="table-status" slot-scope="row">
        {{row.data.item.status | status}}
      </template>
    </InfoTable>
    <!-- Modal Component -->
    <b-modal ref="DefectManagerModal" id="defectManagerModal" centered title="问题解决表" size="lg">
      <InfoTable ref="DefectTable" :location="modal.location" herf="defects" :param="modal.param" :fixed="false" :filterFields="filterFields">
        <template slot="button-operation">
          <b-button size="sm" variant="outline-primary" style="margin-right:6px" @click="defectAssociateManagerModalShow($refs.DefectTable,modal.currentRecord)">
            新建关联
          </b-button>
          <b-button size="sm" variant="outline-primary" style="margin-right:6px" @click="batchRemoveAssociate($refs.DefectTable)">
            批删除关联
          </b-button>
        </template>
        <template slot="table-status" slot-scope="row">
          {{row.data.item.status | status}}
        </template>
        <template slot="table-operation" slot-scope="row">
          <b-btn variant="outline-primary" style="margin-right:8px;padding:6px;font-size:0.7rem" @click="info($refs.DefectInfoModal,row)">信息</b-btn>
          <b-btn variant="outline-primary" style="margin-right:8px;padding:6px;font-size:0.7rem" @click="removeAssociate($refs.DefectTable,row)">删除关联</b-btn>
        </template>
      </InfoTable>
    </b-modal>
    <!-- Info Modal Component-->
    <InfoModal ref="DefectInfoModal" location="amends"></InfoModal>
    <!-- Associate Modal Component -->
    <b-modal ref="DefectAssociateManagerModal" id="defectManagerModal" centered title="添加表" size="lg">
      <InfoTable ref="DefectAssociateTable" :location="modal.location" :param="modal.param" :fixed="false" :filterFields="filterFields">
        <template slot="button-operation">
          <b-button size="sm" variant="outline-primary" style="margin-right:6px" @click="batchAddAssociate($refs.DefectTable)">
            批添加关联
          </b-button>
        </template>
        <template slot="table-status" slot-scope="row">
          {{row.data.item.status | status}}
        </template>
        <template slot="table-operation" slot-scope="row">
          <b-btn variant="outline-primary" style="margin-right:8px;padding:6px;font-size:0.7rem" @click="info($refs.DefectInfoModal,row)">信息</b-btn>
          <b-btn variant="outline-primary" style="margin-right:8px;padding:6px;font-size:0.7rem" @click="addAssociate($refs.DefectTable,row)">添加关联</b-btn>
        </template>
      </InfoTable>
      <div slot="modal-footer" class="w-100">
        <b-btn size="sm" class="float-right" variant="primary" @click="$refs.DefectAssociateManagerModal.hide(),$refs.DefectManagerModal.show()">
          返回
        </b-btn>
      </div>
    </b-modal>
    </div>
</template>

<script>
import {
  baseURL,
  get,
  save,
  associate,
  remove,
  getAndProperty,
  update,
  axios
} from "src/service/api";
import InfoTable from "components/InfoTable";
import InfoModal from "components/InfoModal.vue";
export default {
  name: "Manager",
  components: {
    InfoTable,
    InfoModal
  },
  data() {
    return {
      filterFields: ["status", "type"],
      modal: {
        location: "amends/search/version",
        param: "versionId=1",
        currentRecord: {},
        alterRecord: {},
        status: "create",
        edit: false,
        property: [],
        hasData: false,
        title: "",
        content: "",
        data: [
          {
            label: "",
            description: "",
            type: "",
            key: "",
            name: "",
            value: ""
          }
        ]
      }
    };
  },
  filters: {
    status: function(value) {
      switch (value) {
        case 0:
          return "问题还未提交";
          break;
        case 1:
          return "优化已经提交";
          break;
        case 2:
          return "问题正在解决";
          break;
        case 3:
          return "优化还在分析";
          break;
        case 4:
          return "优化成功";
          break;
        case 5:
          return "优化失败";
          break;
      }
    }
  },
  methods: {
    addAssociate: async function(o, row) {
      let self = this;
      console.log(this.modal.currentRecord);
      console.log(this.getSelfHref(row.data.item));
      console.log(this.getHref(row.data.item, "amend"));
      await o.addAssociate(
        this.getHref(row.data.item, "defect"),
        this.getHref(this.modal.currentRecord.data.item, "defect")
      );
      await get(this.getHref(row.data.item, "amend"))
        .then(response => {
          console.log("zyd");
          if (response.status == "200") {
            console.log("资源获取成功");
            let amend = response.data;
            //先把对应的关联删除
            o.removeAssociate(
              self.getSelfHref(row.data.item),
              self.getHref(amend, "defect")
            );
          }
          console.log(response);
        })
        .then(error => {
          console.log(error);
        });
      //o.addAssociate(
      // this.getSelfHref(this.modal.currentRecord.data.item),
      //this.getHref(row.data.item, "defect")
      // );
    },
    batchAddAssociate(o) {
      console.log(this.modal.currentRecord);
      o.batchAddAssociate(
        this.getSelfHref(this.modal.currentRecord.data.item),
        "defect"
      );
    },
    getAndAssociate(path) {
      return getAndProperty(path);
    },
    removeAssociate(o, row) {
      console.log(this.modal.currentRecord);
      o.removeAssociate(
        this.getSelfHref(row.data.item),
        this.getHref(this.modal.currentRecord.data.item, "defect")
      );
    },
    batchRemoveAssociate(o) {
      console.log(this.modal.currentRecord);
      o.batchRemoveAssociate(
        this.getSelfHref(this.modal.currentRecord.data.item),
        "defect"
      );
    },
    info(o, row) {
      console.log(row);
      o.info(row.data);
    },
    remove(o, row) {
      o.remove(row.data);
    },
    getSelfHref(Record) {
      return Record._links.self.href;
    },
    getHref(Record, name) {
      return Record._links[name].href;
    },
    defectManagerModalShow(o, row) {
      this.modal.currentRecord = row;
      console.log(this.$refs);
      // 获取记录的主键
      let key = parseInt(o.getKey(row.data.item._links.self.href));
      console.log(key);
      // 配置参数
      this.modal.location = `amends/${key}/defect`;
      this.modal.param = "";
      // 打开管理器
      this.$refs.DefectManagerModal.show();
    },
    defectAssociateManagerModalShow(o, row) {
      console.log(this.$refs);
      // 获取记录的主键
      let key = parseInt(o.getKey(row.data.item._links.self.href));
      console.log(key);
      // 配置参数
      this.modal.location = "defects/search/notAmend";
      this.modal.param = "amendId=" + key;
      this.$refs.DefectAssociateTable.refresh();
      // 打开管理器
      this.$refs.DefectAssociateManagerModal.show();
    }
  }
};
</script>
<style lang="scss">
</style>
